﻿package src 
{
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	/**
	 * ...
	 * @author Ivan Lucas Neves
	 */
	public class Player_ship extends MovieClip
	{
		//Bolleans to move and shoot the ship
		var MoveRight	:Boolean;
		var MoveLeft	:Boolean;
		var MoveUp		:Boolean;
		var MoveDown	:Boolean;
		var Shoot		:Boolean;
		
		//Speed of ship, this may change due power ups
		var ShipSpeed 		:int;
		
		
		var bullet :Bullets;
		public var bullets :Array = new Array();
		
		
		public function Player_ship() 
		{
			
			addEventListener(Event.ADDED_TO_STAGE, AddStage, false, 0, true);
			
			ShipSpeed	 	= 5;			
		}

		private function AddStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, AddStage);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, Press, false, 0, true);
			stage.addEventListener(KeyboardEvent.KEY_UP, Release, false, 0, true);
		}
		
		
		public function Update():void 
		{
			moveShip();
			
		}
		
		
		
		private function Press(e:KeyboardEvent):void 
		{
			switch (e.keyCode) 
			{
				case Keyboard.RIGHT:
				MoveRight = true;					
				break;
				
				case Keyboard.LEFT:
				MoveLeft = true;					
				break;
				
				case Keyboard.UP:
				MoveUp = true;					
				break;
				
				case Keyboard.DOWN:
				MoveDown = true;					
				break;
				
				case Keyboard.SPACE:
				Shoot = true;					
				break;
				
			}
			
		}
		
		private function Release(e:KeyboardEvent):void 
		{
			switch (e.keyCode) 
			{
				case Keyboard.RIGHT:
				MoveRight = false;					
				break;
				
				case Keyboard.LEFT:
				MoveLeft = false;					
				break;
				
				case Keyboard.UP:
				MoveUp = false;					
				break;
				
				case Keyboard.DOWN:
				MoveDown = false;					
				break;
				
				case Keyboard.SPACE:
				Shoot = false;					
				break;
				
			}
		}
		
		private function moveShip():void 
		{
			if (MoveRight == true)
			{
				this.x += ShipSpeed;
			}
			
			if (MoveLeft == true)
			{
				this.x -= ShipSpeed;
			}
			
			if (MoveUp == true)
			{
				this.y -= ShipSpeed;
			}
			
			if (MoveDown == true)
			{
				this.y += ShipSpeed;
			}
			
			if (Shoot == true) 
			{
				shipShoot();
			}
			
		}
		
		private function shipShoot():void 
		{
			bullet = new Bullets();
			bullet.x =	this.x / 2;
			bullet.y =	this.y / 2 ;
			addChildAt(bullet, 0);
			//bullets.push(bullet);
		}
		
		
		
	}

}